package tor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import protocol.Info;
import data.HashedCard;

public class ReceivingVotesServer
{
    public static void main( String[] args ) throws IOException, InterruptedException
    {
        ServerSocket ss = new ServerSocket( Info.TOR_PORT );
        while( true )
        {
            final Socket cs = ss.accept();
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        InputStream is = cs.getInputStream();
                        BufferedReader in = new BufferedReader( new InputStreamReader( is ) );
                        String line = in.readLine();
                        HashedCard hasheddatabasecard = new HashedCard( line );
                        System.out.println( "Vote received: " + line );
                        System.out.println( "---------" );
                        if( line != null )
                        {
                            System.out.println( line );
                            // System.out.println( "SERV4" );
                            // String qwe = "Howdy cowboy!\r\n";
                            // byte[] requestBytes;
                            // requestBytes = line.getBytes( "UTF8" );
                            // cs.getOutputStream().write( requestBytes );
                            // cs.getOutputStream().flush();
                            // System.out.println( "SERV5" );
                            // Thread.sleep( 1000 );
                        }
                        else
                        {
                        }
                        // Thread.sleep(30000);
                        // receive data (e.g. HTTP request) and send data (e.g.
                        // HTTP response)
                        // hint: to avoid dead locks: use separate threads for
                        // each direction
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
}
